package com.blkan.calculator.fragments;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupMenu;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.blkan.calculator.MainActivity;
import com.blkan.calculator.R;
import com.blkan.calculator.databinding.HistoryItemBinding;
import com.blkan.calculator.history.HistoryItem;
import com.blkan.calculator.history.HistoryRepository;
import com.blkan.calculator.util.SqliteHelper;

import java.util.List;

public class HistoryItemAdapter extends RecyclerView.Adapter<HistoryItemAdapter.ViewHolder> {

    private final HistoryFragment fragment;
    private final List<HistoryItem> historyItemList;

    public HistoryItemAdapter(HistoryFragment fragment) {
        this.fragment = fragment;
        this.historyItemList = HistoryRepository.getInstance().getHistoryItems();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.history_item, parent, false);
        return new ViewHolder(view);
    }

    @SuppressLint("NotifyDataSetChanged")
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        HistoryItem item = historyItemList.get(position);
        holder.binding.setHistoryItem(item);
        holder.binding.getRoot().setOnClickListener(view -> {
            PopupMenu menu = new PopupMenu(fragment.getContext(), view);
            menu.getMenuInflater().inflate(R.menu.menu_history_item, menu.getMenu());
            menu.setOnMenuItemClickListener(menuItem -> {
                if(menuItem.getItemId() == R.id.menu_recalculate)
                {
                    Toast.makeText(fragment.getContext(), holder.binding.getHistoryItem().toString(), Toast.LENGTH_LONG).show();
                    if(fragment.getActivity() instanceof MainActivity)
                    {
                        MainActivity activity = (MainActivity) fragment.getActivity();
                        int actionId = R.id.action_dest_history_fragment_to_dest_calculator_fragment;
                        Bundle args = new HistoryFragmentArgs.Builder(holder.binding.getHistoryItem()).build().toBundle();
                        activity.getNavHostFragment().getNavController().navigate(actionId, args);
                    }
                    return true;
                }
                if (menuItem.getItemId() == R.id.menu_delete)
                {
                    SqliteHelper helper = new SqliteHelper(fragment.getContext());
                    helper.delete(historyItemList.get(position).getUuid());
                    helper.readAndStoreData();
                    notifyDataSetChanged();
                    return true;
                }
                if (menuItem.getItemId() == R.id.menu_delete_all)
                {
                    SqliteHelper helper = new SqliteHelper(fragment.getContext());
                    helper.deleteAll();
                    helper.readAndStoreData();
                    notifyDataSetChanged();
                    return true;
                }
                return false;
            });
           menu.show();
        });

    }

    @Override
    public int getItemCount() {
        return historyItemList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private final HistoryItemBinding binding;

        public ViewHolder(@NonNull View view) {
            super(view);
            binding = HistoryItemBinding.bind(view);
        }

        public HistoryItemBinding getBinding() {
            return binding;
        }
    }
}
